package com.oversea.action.user;

import static com.oversea.util.ParamUtil.isNotEmpty;
import java.util.Date;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.oversea.action.BaseAction;
import com.oversea.annotation.Permission;
import com.oversea.models.User;
import com.oversea.models.UserAction;
import com.oversea.service.UserActionService;
import com.oversea.service.UserService;
import com.oversea.util.BeanUtils;
import com.oversea.util.page.Page;
import com.oversea.util.page.PageUtil;
import com.oversea.util.searchcontext.SearchContext;
import com.oversea.util.searchcontext.SearchOption;

@ParentPackage("oversea-default")
@InterceptorRefs( { @InterceptorRef("defaultStack") })
@Namespace("/user")
@Permission(level = 3)
public class UserActions extends BaseAction {

	private static final long serialVersionUID = -8199478948374178740L;

	private UserService userService;

	private UserActionService userActionService;

	private User user;

	private List<User> userList;

	private List<UserAction> userActionList;

	private Page page;

	private int objId;

	private String userName;

	@SuppressWarnings("unchecked")
	public Class getActionClass() {
		return User.class;
	}

	private SearchContext getListSearchContext() {
		SearchContext searchContext = new SearchContext();
		if (isNotEmpty(userName)) {
			searchContext.addOption(new SearchOption("userName", "%" + userName
					+ "%", SearchOption.Option.like));
		}
		searchContext.setSearchClass(getActionClass());
		return searchContext;
	}

	@Action(value = "toadd", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/user/user_add.ftl") })
	public String add() {
		return SUCCESS;
	}

	@Action(value = "toedit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/user/user_edit.ftl") })
	public String edit() {
		user = userService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "toview", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/user/user_view.ftl") })
	public String view() {
		user = userService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "toviewactions", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/user/user_view_actions.ftl") })
	public String viewActions() {
		user = userService.getObjById(objId);
		SearchContext searchContext = new SearchContext();
		searchContext.addOption(new SearchOption("user.id", user.getId(),
				SearchOption.Option.eqaul));
		searchContext.setSearchClass(UserAction.class);
		page = PageUtil.createPage(pageSize, currentPage, userActionService
				.getObjectCount(searchContext).intValue());
		userActionList = userActionService.getObjectList(searchContext, pageSize,
				currentPage);
		return SUCCESS;
	}

	@Action(value = "add", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String save() {
		user.setCreatedTime(new Date());
		user.setModifiedTime(new Date());
		userService.save(user);
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String update() {
		User temp = userService.getObjById(user.getId());
		user.setCreatedTime(temp.getCreatedTime());
		user.setModifiedTime(new Date());
		BeanUtils.copyProperties(temp, user);
		userService.update(temp);
		user = temp;
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/user/user_list.ftl") })
	public String list() {
		User searchUser = new User();
		searchUser.setUserName(userName);
		user = searchUser;
		page = PageUtil.createPage(pageSize, currentPage, userService
				.getObjectCount(getListSearchContext()).intValue());
		userList = userService.getObjectList(getListSearchContext(), pageSize,
				currentPage);
		return SUCCESS;
	}

	@Action(value = "del", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String del() {
		userService.deleteById(objId);
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<UserAction> getUserActionList() {
		return userActionList;
	}

	public void setUserActionList(List<UserAction> userActionList) {
		this.userActionList = userActionList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserActionService getUserActionService() {
		return userActionService;
	}

	public void setUserActionService(UserActionService userActionService) {
		this.userActionService = userActionService;
	}
}
